#include <iostream>
#include <iostream>
#include <unistd.h>
#include <cstdio>
#include <string>
#include <cerrno>
#include <cstring>
#include <cstdlib>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
using namespace std;
#define SIZE 1024
void Usage(string proc)
{
    cout << "Usage" << proc << "client_ip"
         << " "
         << "client_port" << endl;
}

int main(int argc, char *argv[])
{
    if (argc != 3)
    {
        Usage(argv[0]);
    }
    int sock = socket(AF_INET, SOCK_DGRAM, 0); // 创建套接字
    // 客户端发送数据
    struct sockaddr_in server;
    memset(&server, 0, sizeof(server));
    server.sin_family = AF_INET;
    server.sin_port = htons(atoi(argv[2]));      // 填充服务器的端口号
    server.sin_addr.s_addr = inet_addr(argv[1]); // 填充服务器的ip
    char buffer[SIZE];
    while (true)
    {
        string message;
        cout << "请输入你的信息#" << endl;
        getline(cin, message);
        if(message=="quit")
        {
            break;
        }
        
        sendto(sock, message.c_str(), message.size(), 0, (sockaddr *)&server, sizeof(server));
        struct sockaddr_in temp;
        bzero(&temp, 0);
        socklen_t len = sizeof(temp);
        ssize_t s = recvfrom(sock, buffer, sizeof(buffer)-1, 0, (sockaddr *)&temp, &len);
        if (s > 0)
        {
            buffer[s]=0;
            cout << "client echo->" << buffer << endl;
        }
    }
    close(sock);
    return 0;
}